package com.mediacloud.app.newsmodule.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.mediacloud.app.assembly.util.jsonToArrayList
import com.mediacloud.app.model.MagazineEventBusData
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.model.fragment.BaseFragment
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.MagazineProgramAdapter
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.newsmodule.model.MagazineProgramBean
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_magazine_program_list.*
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

class MagazineProgramFragment : BaseFragment(){

    var pageNum = 1
    var mAdapter: MagazineProgramAdapter? = null
    val fatherId by lazy {
        arguments?.getString("magazineID", "") ?: ""
    }

    companion object {

        fun getInstance(fatherId: String?): MagazineProgramFragment {
            val fragment = MagazineProgramFragment()
            val bundle = Bundle()
            bundle.putString("magazineID", fatherId)
            fragment.arguments = bundle
            return fragment
        }
    }
    override fun getLayoutResID(): Int {
        return R.layout.fragment_magezine_program_layout
    }

    override fun initView() {


        showStateView(BaseBackActivity.TYPE_LOADING, false)
        rvList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mAdapter = MagazineProgramAdapter()
        rvList.adapter = mAdapter
        loadingView = findViewById(R.id.mLoadingView)
        initStateView()
        loadingImage.setOnClickListener {
            if (currentState != BaseBackActivity.TYPE_LOADING) {
                showStateView(BaseBackActivity.TYPE_LOADING, false)
                pageNum = 1
                getList()
            }
        }
        getList()
    }


    private fun getList() {

        SpiderCmsApi.getSpiderCmsApiJson(context)
            .getChildMagazineList(fatherId, 1, 20)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<JSONObject> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: JSONObject) {


                    t.let {
                        val data = it.optJSONObject("data")
                        val state = it.optInt("state")
                        EventBus.getDefault().post(MagazineEventBusData(data))
                        if (state == 200) {
                            val optJSONArray = data.optJSONArray("childCatalog")
                            val taskList = jsonToArrayList(
                                optJSONArray.toString(),
                                MagazineProgramBean::class.java
                            )
//                            val taskList = FAST.parseArray(
//                                optJSONArray.toString(),
//                                MagazineProgramBean::class.java
//                            ) as ArrayList
                            if (taskList.size > 0) {
                                var bean: MagazineProgramBean? = null
                                var i = 0
                                while (i < taskList.size) {
                                    bean = taskList.get(i)
                                    if (bean?.titleShowFlag != 1
                                        && (bean.articlePageRecords == null
                                                || bean.articlePageRecords.size == 0)
                                    ) {
                                        taskList.remove(bean)
                                    } else {
                                        i++
                                    }
                                }
                                closeStateView()
                                if (pageNum == 1) {
                                    mAdapter?.setListData(taskList)
                                } else {
                                    mAdapter?.addListData(taskList)
                                }
                                mAdapter?.notifyDataSetChanged()
                            } else {
                                showStateView(BaseBackActivity.TYPE_NO_CONTENT, false)
                                closeStateView()
                            }
                        } else {
                            showStateView(BaseBackActivity.TYPE_NET_NOT_GIVE_FORCE, false)
                            closeStateView()
                        }
                    }
                }

                override fun onError(e: Throwable) {
                    //  if (ivHeadImg.visibility != View.VISIBLE)
                    //     showStateView(TYPE_NET_NOT_GIVE_FORCE, false)
                    //else
                    closeStateView()
                }

                override fun onComplete() {
                }
            })

    }
    interface DataCallBack{
        fun callBack()
    }
}